package com.atguigu.gulimall.search.thread;

import java.util.concurrent.*;

/**
 * @ ClassName ThreadTest
 * @ Description TODO
 * @ Author admin
 * @ Date 2021/3/15 14:34
 * @ Version 1.0
 */
public class ThreadTest {
    //线程池
    public static ExecutorService execute = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main方法启动......");
        //无返回值
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程id: " + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果: " + i);
//        }, execute);

        //返回值
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程id: " + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果: " + i);
//            return i;
        /**
         * 方法完成后的处理
         */
//        }, execute).whenCompleteAsync((result,exception)->{
//            System.out.println("运行结果: "+result+"异常为:"+exception);
//        }).exceptionally(method->{
//            //处理异常时返回的结果
//            return 10;
//        });
//        System.out.println("main方法结束......"+future.get());

       CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程id: " + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果: " + i);
            return i;
        }, execute).handle((result,exception)->{
            if (result != null){
                return result *=2;
            }
            if (exception !=null){
                return 0;
            }
            return  -1;
       });
        System.out.println("main方法结束......"+future.get());
    }

    public void thread(String[] args) throws ExecutionException, InterruptedException {
//        Thread01 thread01 = new Thread01();
//        thread01.start();
//        Runnable01 runnable01 = new Runnable01();
//        new Thread(runnable01).start();
//
//        FutureTask<Integer> integerFutureTask = new FutureTask<>(new Callable01());
//        new Thread(integerFutureTask).start();
//
//        Integer integer = integerFutureTask.get();
        //线程池方式
        execute.execute(new Runnable01());
        /**
         * Creates a new {@code ThreadPoolExecutor} with the given initial
         * parameters.
         *
         * @param corePoolSize the number of threads to keep in the pool, even
         *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
         *        核心线程数.
         * @param maximumPoolSize the maximum number of threads to allow in the
         *        pool
*               最大线程数量
         * @param keepAliveTime when the number of threads is greater than
         *        the core, this is the maximum time that excess idle threads
         *        will wait for new tasks before terminating.
         *        存活时间,如果当前线程数量大于核心数量,那么就释放多余的线程,只要线程空闲时间大于指定的keepAliveTime
         *
         * @param unit the time unit for the {@code keepAliveTime} argument
         *        时间单位
         * @param workQueue the queue to use for holding tasks before they are
         *        executed.  This queue will hold only the {@code Runnable}
         *        tasks submitted by the {@code execute} method.
         *        阻塞队列,如果任务很多.就将任务放到阻塞队列中
         * @param threadFactory the factory to use when the executor
         *        creates a new thread
         *        线程的创建工厂
         * @param handler the handler to use when execution is blocked
         *        because the thread bounds and queue capacities are reached
         *        线程任务满了的拒绝策略
         * @throws IllegalArgumentException if one of the following holds:<br>
         *         {@code corePoolSize < 0}<br>
         *         {@code keepAliveTime < 0}<br>
         *         {@code maximumPoolSize <= 0}<br>
         *         {@code maximumPoolSize < corePoolSize}
         * @throws NullPointerException if {@code workQueue}
         *         or {@code threadFactory} or {@code handler} is null
         */
        /**
         * 工作顺序
         * 1.1 线程池创建,准备好核心线程数量,准备接受任务
         * 1.2 线程池满了,就将任务放在阻塞队列中,阻塞队列满就开线程直到最大线程
         * 1.3 最大线程满,就执行拒绝策略
         * 1.4 释放空闲的线程
         */
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5,
                100,
                60,
                TimeUnit.SECONDS,new LinkedBlockingDeque<>()
        );

    }

    public static class Thread01 extends Thread{

        @Override
        public void run() {
            super.run();
            System.out.println("Thread01启动.....");
        }
    }

    public static class Runnable01 implements Runnable{

        @Override
        public void run() {
            System.out.println("runnable01线程");
        }
    }

    public static class Callable01 implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            return 1;
        }
    }
}
